<?php
function renderChild($md, $idcat, &$count, $link, $level=1){
    $childs = $md->fetchAll("parent_id='$idcat'");
    if($childs->count()<=0){
        return;
    }
    $tab = "";
    for($i=1; $i<$level; $i++){
        $tab.="&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;";
    }
    if($tab !=""){
        $tab="<span class='separator'>|</span>$tab";
    }
    $view = Zend_Layout::getMvcInstance()->getView();
    $baseUrl = $view->baseUrl();    
    
    foreach($childs as $child){
        $count++;
        $ico = ($child->category_status)?"star_list.png":"star_unlist.png";
        $title = ($child->category_status)?"Ẩn":"Hiện";
        $alt = ($child->category_status)?"Hiện":"Ẩn";
        echo 
        "<tr class='gradeU odd'>
            <td >$count<input type='hidden' value='$child->idcategories' /></td>
            <td style='color:#204562;'>
                <div style='position:relative;'>
                    $tab<span class='separator'>|</span><span class='tree-border'></span>$child->category_name
                </div>
            </td>
            <td class='center'>
                <a href='$link/list/choice/multi/idcat/$child->idcategories' class='choice-multi'>
                    <img src='$baseUrl/templates/admin/img/$ico' alt='$alt' title='$title' />
                </a>
            </td>
            <td>
                <a href='$link/edit/idcat/$child->idcategories' class='action-edit'>
                    <img src='$baseUrl/templates/admin/img/icon-edit.png' alt='edit' title='Sửa' />
                </a>
            </td>
            <td>
                <a href='$link/del/idcat/$child->idcategories' rel='$child->idcategories' class='action-del'>
                    <img src='$baseUrl/templates/admin/img/icon-del.png' alt='delete' title='Xóa' />
                </a>
            </td>
            <td>
                <input class='change-local' type='text' value='$child->category_local' size='2'/>
            </td>
        </tr>";
        renderChild($md, $child->idcategories, $count, $link, $level+1);
    }
}
?>
<div class="grid_12">
    <div class="box round first fullpage">
        <h2>Danh sách các danh mục</h2>
        <div class="block">
            <div class="message" style="display: none;">
                <h5>Warning!</h5>
                <p>
                    This is a warning message.
                </p>
            </div>
            <br style="margin-bottom: 20px;" />
            <div class="dataTables_wrapper">
                <table class="data display datatable">
                    <thead>
                        <tr>
                            <th width="40" class="center">STT</th>
                            <th class="left">Tên danh mục</th>
                            <th width="60" class="center">Hiện/Ẩn</th>
                            <th width="50" class="center">Sửa</th>
                            <th width="50" class="center">Xóa</th>
                            <th width="60" class="center">local</th>
                        </tr>                   
                    </thead>
                    <tbody>
                        <?php
                        $link = $this->baseUrl("admin/article-category");
                        $baseUrl = $this->baseUrl();
                        $md = new Application_Model_DbTable_ArticleCategory();
                        $parents = $md->fetchAll("parent_id IS NULL");
                        $stt = 1;
                        foreach($parents as $parent){
                            $level = 0;
                            $ico = ($parent->category_status)?"star_list.png":"star_unlist.png";
                            $title = ($parent->category_status)?"Ẩn":"Hiện";
                            $alt = ($parent->category_status)?"Hiện":"Ẩn";
                            echo 
                            "<tr class='gradeU odd'>
                                <td>$stt<input type='hidden' value='$parent->idcategories' /></td>
                                <td><div style='position:relative;'>$parent->category_name</td>
                                
                                <td class='center'>
                                    <a href='$link/list/choice/multi/idcat/$parent->idcategories' class='choice-multi'>
                                        <img src='$baseUrl/templates/admin/img/$ico' alt='$alt' title='$title' />
                                    </a>
                                </td>
                                
                                <td>
                                    <a href='$link/edit/idcat/$parent->idcategories' class='action-edit'>
                                        <img src='$baseUrl/templates/admin/img/icon-edit.png' alt='edit' title='Sửa' />
                                    </a>
                                </td>
                                <td>
                                    <a href='$link/del/idcat/$parent->idcategories' rel='$parent->idcategories' class='action-del'>
                                        <img src='$baseUrl/templates/admin/img/icon-del.png' alt='delete' title='Xóa' />
                                    </a>
                                </td>
                                <td>
                                    <input class='change-local' type='text' value='$parent->category_local' size='2'/>
                                </td>
                            </tr>";
                            renderChild($md,$parent->idcategories,$stt,$link);
                            $stt++;
                        }
                        ?>
                    </tbody>
                </table>
            </div>
            <br />
            <a href="<?=$this->url(array('module'=>"admin",'controller'=>"article-category",'action'=>"add"),null , true)?>" class="btn btn-black">Thêm mới</a>            
        </div>
    </div>
</div>
<div class="dialog-message" title="Thông báo" style="display: none;"></div>
<script>
$(function(){
    //delete item
    $(".action-del").click(function(){
        var link = $(this).attr("href");
        var _click = $(this);
        $(".dialog-message").html("<div class='message warning'><h5>Cảnh báo!</h5><p>Bạn có chắc muốn làm điều này không?</p><div>").dialog({
            autoOpen: true,
            modal: true,
            buttons:{
                "Có": function(){
                    $.get(link,null,function(rdata){
                        if(rdata!=0){
                            $(".message").addClass("warning").show();
                            $(".message h5").text("Cảnh báo!");
                            $(".message p").text("Tồn tại danh mục con hoặc có bài viết trong danh mục này. Không thể xóa.");
                            window.location = "javascript:scroll(0,0)";
                            setInterval(function(){
                                $(".message").fadeOut(1000)
                            },4000);
                        }else{
                            _click.parent("td").parent("tr").remove();
                            window.location.reload();
                        }
                    },'json');
                    $(this).dialog("close");
                },                
                "Không": function(){
                    $(this).dialog("close");
                }
            }
            
        })
        return false;
    });
    
    $(".change-local").change(function(){
        var lc = $(this).val();
        var id = $(this).parent("td").parent("tr")
                        .find("td:first")
                        .children("input[type='hidden']").val();
        var link = "<?=$this->url(array('module'=>"admin",'controller'=>"article-category",'action'=>"list"),null, true)?>";
        $.post(link,{"act":"local","idcat":id,"local":lc},function(){},"json");
    })
    
    $(".choice-multi").click(function(){
        var load = $("<div class='message warning loading'>Saving...</div>");
        load.show();
        $(".block").css("position","relative").prepend(load);
        var status = $(this).attr("rel");
        var link = $(this).attr("href");
        $.get(link,{},function(){
            window.location.reload();
        },"json");
        return false;
    })
})
</script>